<template>
  <div class="global-header">
    <div class="left">
      <i
        class="icon iconfont trigger"
        :class="{ 'icon-sider-close': !isCollapse, 'icon-sider-open': isCollapse }"
        @click="handleCollapse"
      ></i>
    </div>
    <div class="right">
      <el-button type="text" icon="el-icon-s-promotion" @click="goDashboard">
        数据大盘
      </el-button>
      <el-dropdown v-if="userInfo" size="medium" @command="handleCommand">
        <div class="action">
          <div class="userinfo">
            <span>{{ userInfo.name }}</span>
            <!-- <span class="role">{{ user.position }}</span> -->
          </div>
          <div v-if="userInfo.avatar" class="avatar">
            <img :src="userInfo.avatar" alt="avatar" />
          </div>
        </div>
        <el-dropdown-menu slot="dropdown" class="account-dropdown">
          <!-- <el-dropdown-item command="changePwd">
						<i class="iconfont icon-setting"></i>修改密码
					</el-dropdown-item>-->
          <el-dropdown-item command="logout">
            <i class="iconfont icon-logout"></i>退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import { mapGetters, mapState, mapActions, mapMutations } from 'vuex';
import store from '@/store';

export default {
  // inject: ['reload'],
  data() {
    return {
      curAppId: '123',
      isCollapse: false,
      showAppSelector: false,
    };
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.info,
    }),

    layerVisible() {
      return (
        this.orgs.length == 0 ||
        !this.orgs.find(item => {
          return item.isSelected;
        })
      );
    },
  },
  watch: {
    $route(newVal) {
      const { path } = newVal;
      if (path.match(/^\/count/)) {
        this.showAppSelector = false; // TODO暂时都隐藏
      } else {
        this.showAppSelector = false;
      }
    },
  },
  methods: {
    ...mapMutations(['setState']),

    ...mapActions('login', ['logout']),
    refresh() {
      window.location.reload();
    },
    openSelectOrg() {
      store.dispatch('setOrgSelect', true);
    },
    closeSelectOrg() {
      //当前没有选中的机构 不能关闭
      if (
        !this.orgs.some(item => {
          return item.isSelected;
        })
      )
        return;
      store.dispatch('setOrgSelect', false);
    },
    handleCommand(command) {
      if (command == 'logout') {
        this.logout();
      }
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
      this.$emit('onCollapse', this.isCollapse);
    },
    handleOrgChange(value) {
      this.curAppId = value;
    },
    goDashboard() {
      this.$router.push('/dashboard');
    },
  },
};
</script>

<style lang="less" scoped>
.global-header {
  .left {
    flex: 1 1 auto;
    .icon {
      font-size: 25px;
    }
  }
  .middle {
    flex: 0 0 auto;
  }
  .right {
    flex: 1 1 auto;
    justify-content: flex-end;
  }
  /deep/ .el-dialog__body {
    height: auto;
  }
  /deep/ .app-selector .el-input__inner {
    border: none;
    font-weight: bold;
  }
}
.layer-bg-white {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: #ccc;
  z-index: -100;
}
.org-item {
  margin: 0 auto 20px auto;
  display: block;
}
.global-header {
  height: 64px;
  padding: 0 12px 0 0;
  background: #fff;
  -webkit-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  -moz-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  -ms-box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

i.trigger {
  font-size: 20px;
  line-height: 64px;
  cursor: pointer;
  transition: all 0.3s;
  padding: 0 24px;
  display: inline-block;
  &:hover {
    background-color: #e6f7ff;
  }
}
.right {
  display: flex;
  align-items: center;
  height: 100%;
  .el-dropdown {
    height: 100%;
  }
}
.action {
  cursor: pointer;
  padding: 0 12px;
  display: flex;
  align-items: center;
  transition: all 0.3s;
  height: 100%;
  i {
    height: auto;
  }
  &:hover {
    background-color: #e6f7ff;
  }
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  overflow: hidden;
  margin-left: 10px;
  img {
    width: 100%;
    height: 100%;
    display: block;
  }
}
.userinfo {
  display: flex;
  flex-direction: column;
  .role {
    font-size: 12px;
  }
}
.account-dropdown {
  padding: 0 12px;
  word-break: keep-all;
  .iconfont {
    margin-right: 6px;
  }
}
</style>
